
package com.mancj.slideup;

import ohos.agp.animation.Animator;
import ohos.agp.animation.AnimatorValue;

/**
 * Animation processor
 *
 * @author pa.gulko zTrap (12.07.2017)
 */
class AnimationProcessor {
    /**
     * The constant M builder
     */
    private SlideUpBuilder mBuilder;
    /**
     * The constant M value animator
     */
    private ValueAnimator mValueAnimator;
    /**
     * The constant M slide animation to
     */
    private float mSlideAnimationTo;

    /**
     * Animation processor
     *
     * @param builder        builder
     * @param updateListener update listener
     * @param listener       listener
     */
    AnimationProcessor(SlideUpBuilder builder, AnimatorValue.ValueUpdateListener updateListener, Animator.StateChangedListener listener) {
        mBuilder = builder;
        createAnimation(updateListener, listener);
    }

    /**
     * End animation
     */
    void endAnimation() {
        if (mValueAnimator != null && mValueAnimator.isRunning()) {
            mValueAnimator.end();
        }
    }

    /**
     * Params changed
     */
    void paramsChanged() {
        mValueAnimator.setDuration(mBuilder.mAutoSlideDuration);
        mValueAnimator.setInterpolator(mBuilder.mInterpolator);
    }

    /**
     * Get slide animation to float
     *
     * @return the float
     */
    float getSlideAnimationTo() {
        return mSlideAnimationTo;
    }

    /**
     * Is animation running boolean
     *
     * @return the boolean
     */
    boolean isAnimationRunning() {
        return mValueAnimator != null && mValueAnimator.isRunning();
    }

    /**
     * Set values and start *
     *
     * @param from from
     * @param to   to
     */
    void setValuesAndStart(float from, float to) {
        mSlideAnimationTo = to;
        mValueAnimator.setFloatValues(from, to);
        mValueAnimator.start();
    }

    /**
     * Create animation *
     *
     * @param updateListener update listener
     * @param listener       listener
     */
    private void createAnimation(AnimatorValue.ValueUpdateListener updateListener, Animator.StateChangedListener listener) {
        mValueAnimator = ValueAnimator.ofFloat();
        mValueAnimator.setDuration(mBuilder.mAutoSlideDuration);
        mValueAnimator.setInterpolator(mBuilder.mInterpolator);
        mValueAnimator.setValueUpdateListener(updateListener);
        mValueAnimator.setStateChangedListener(listener);
    }
}
